package tq.com.tqteacher.adapt.newPublish;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.print.PrinterId;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.vh.CommonViewHolder;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQUserInfo;

public class TQLetShareContentAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private ArrayList<TQAtachModel> mAtachModels = null;
    private int all;
    private int upload;

    public TQLetShareContentAdapter(Context pContext){
        mContext = pContext;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        if(pI == 1){
            return CommonViewHolder.sCommonViewHolder(mContext, R.layout.vh_letsharecapture_layout, pViewGroup)
                    .setClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            if(upload >= all){
//                                AlertDialogHelper.show(mContext, "已达上传上限");
//                                return;
//                            }
                            if(mClickListener != null){
                                mClickListener.capture();
                            }
                        }
                    }).setHolderBinder(new CommonViewHolder.IViewHolder() {
                        @Override
                        public void bind(View pView, Object data) {
                                TextView lTextView = pView.findViewById(R.id.alreadyUpload);
                                TextView lTextView1 = pView.findViewById(R.id.allAtt);

                                lTextView.setText(String.format("已上传:%d", upload));
                                lTextView1.setText(String.format("人数上限:%d", all));

//                                if(upload >= all){
//                                    pView.findViewById(R.id.upload).setEnabled(false);
//                                }
                        }
                    });
        }else{
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_letshare_picitem_layout, pViewGroup, false);
            return new CaptureDetailViewHold(lView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pViewHolder instanceof CaptureDetailViewHold){
            TQAtachModel lAtachModel = mAtachModels.get(pI - 1);
            ((CaptureDetailViewHold)pViewHolder).bind(pI - 1, lAtachModel);
        }else if(pViewHolder instanceof CommonViewHolder){
            ((CommonViewHolder)pViewHolder).bindData(new Object());
        }
    }

    @Override
    public int getItemCount() {
        return getItemRealCount() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0) return 1;
        return 0;
    }

    public int getItemRealCount(){
        return mAtachModels != null ? mAtachModels.size() : 0;
    }

    public void replaceAttach(ArrayList<TQAtachModel> pTQAtachModels){
        mAtachModels = pTQAtachModels;
        notifyDataSetChanged();
    }

    public void updateAttanchInfo(int pAll, int pUpload){
        all = pAll;
        upload = pUpload;

        notifyItemChanged(0);
    }
    public void updateAttachInfo(TQAtachModel pModel){
        int index = mAtachModels.indexOf(pModel);
        if(index > 0){
            notifyItemChanged(index + 1);
        }
    }

    public TQAtachModel getAttachById(int id){
        if(id == 0) return null;

        for (TQAtachModel a: mAtachModels
             ) {
            if(a != null && a.getId() == id){
                return a;
            }
        }
        return null;
    }

    class CaptureDetailViewHold extends RecyclerView.ViewHolder{
        private TextView no;
        private ImageView userIcon;
        private TextView userName;

        private TextView looker;
        private TextView liuyan;

        private TextView mTitle;
        private GridView pictures;

        private PictureAdapter mPictureAdapter = new PictureAdapter();
        private TQAtachModel mAtachModel;

        public CaptureDetailViewHold(View pView){
            super(pView);

            no = pView.findViewById(R.id.index_no);
            userIcon = pView.findViewById(R.id.user_icon);
            userName = pView.findViewById(R.id.user_name);

            looker = pView.findViewById(R.id.lookerCount);
            liuyan =pView.findViewById(R.id.levaeMsgCount);

            mTitle = pView.findViewById(R.id.desc);
            pictures = pView.findViewById(R.id.picture);

            pictures.setAdapter(mPictureAdapter);
            pictures.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if(mClickListener != null){
                        if(mAtachModel == null) return;
                        mClickListener.openPicture(mAtachModel.getFiles().get(position));
                    }
                }
            });
            pictures.setSelector(new ColorDrawable(Color.TRANSPARENT));
            pictures.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    float width = pictures.getWidth();
                    float w = pictures.getMeasuredWidth();

                    float item = (width - 3 * 15 * mContext.getResources().getDisplayMetrics().density) / 4.0f;
                    mPictureAdapter.setItemSize(item);
                    Log.d("onGlobalLayout", "onGlobalLayout: ");
                    pictures.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            });

            pView.findViewById(R.id.levaeMsgIcon).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mClickListener != null && mAtachModel != null){
                        mClickListener.openAttachComm(mAtachModel.getId());
                    }
                }
            });
        }

        public void bind(int position, TQAtachModel pTQAtachModel){
            int index = getItemRealCount() - position;
            no.setText(String.format("第%d份", index));

            mAtachModel = pTQAtachModel;
            if(mAtachModel == null) return;

            TQUserInfo lTQUserInfo = mAtachModel.getUserInfo();
            if(lTQUserInfo == null){
                lTQUserInfo = TQLogin.getCurrent().getUserInfo();
            }

            RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.tx3).circleCrop();
            String url = TQPictureUpload.downloadPath(lTQUserInfo.getPic());
            Glide.with(mContext).load(url).apply(lOptions).into(userIcon);

            userName.setText(lTQUserInfo.getName());
            looker.setText(String.valueOf(mAtachModel.getReadCount()));
            liuyan.setText(String.valueOf(mAtachModel.getCommentCount()));

            mTitle.setText(mAtachModel.getDesc());

            mPictureAdapter.updateData(mAtachModel.getFiles());

        }
    }

    class PictureAdapter extends BaseAdapter{
        private ArrayList<String> pics ;
        private float itemSize  = 0;

        public void updateData(ArrayList<String> pPics){
            pics = pPics;
            notifyDataSetChanged();
        }
        public void setItemSize(float pItemSize){
            itemSize = pItemSize;
            notifyDataSetChanged();
        }
        @Override
        public Object getItem(int position) {
            return pics != null ? pics.get(position) : null;
        }

        @Override
        public int getCount() {
            return pics != null ? pics.size() : 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ImageView lView = (ImageView)convertView;
            if(lView == null){
                lView = new ImageView(mContext);
            }
            lView.setClickable(false);
            String url = TQPictureUpload.downloadPath(pics.get(position));

            RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.ic_default_picture)
                    .override((int)(itemSize), (int)(itemSize))
                    .centerCrop();
            Glide.with(mContext).load(url).apply(lOptions)
                    .into(lView);

            return lView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }

    public interface IClickListener{
        void capture();
        void openPicture(String id);

        void openAttachComm(int id);
    }

    public void setClickListener(IClickListener pClickListener){
        mClickListener = pClickListener;
    }
    private IClickListener mClickListener = null;
}
